package com.bwie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.domain.Groupbuyingactivity;
import com.bwie.domain.UserGroup;
import com.bwie.mapper.UserGroupMapper;
import com.bwie.service.GroupbuyingactivityService;
import com.bwie.mapper.GroupbuyingactivityMapper;
import com.bwie.utils.R;
import com.bwie.vo.GroupVo;
import com.bwie.vo.PageInfoVo;
import org.checkerframework.checker.guieffect.qual.UI;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* @author 36167
* @description 针对表【groupbuyingactivity(团购活动表)】的数据库操作Service实现
* @createDate 2025-03-19 10:25:54
*/
@Transactional
@Service
public class GroupbuyingactivityServiceImpl extends ServiceImpl<GroupbuyingactivityMapper, Groupbuyingactivity>
    implements GroupbuyingactivityService{

    @Autowired
    GroupbuyingactivityMapper groupbuyingactivityMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    UserGroupMapper userGroupMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public R grouplist(PageInfoVo pageInfoVo) {
        Page<Groupbuyingactivity> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        Page<Groupbuyingactivity> page1 = groupbuyingactivityMapper.selectPage(page, null);
        return R.OK(page1);
    }

    @Override
    public R join(Integer uid, Integer gid, Integer goodsid) {

        String lock="lock:"+uid+":"+goodsid;
        Boolean b = stringRedisTemplate.opsForValue().setIfAbsent(lock, uid + "", 10, TimeUnit.SECONDS);
        if (!b){
            return R.ERROR(500,"加团失败");
        }

        Long decrement = stringRedisTemplate.opsForValue().decrement(goodsid + "");
        if (decrement<0){
            stringRedisTemplate.opsForValue().increment(goodsid+"");
            return R.ERROR(500,"加团成员大于成员条件人数");
        }

        UserGroup userGroup = new UserGroup();
        userGroup.setUid(uid);
        userGroup.setGid(gid);
        int insert = userGroupMapper.insert(userGroup);
        return insert>0?R.OK("加团成功"):R.ERROR(500,"加团失败");

    }

    @Override
    public R groupadd(Groupbuyingactivity groupbuyingactivity) {
        int i=0;
        String s = RandomUtil.randomNumbers(4);
        groupbuyingactivity.setGnum(s);
        groupbuyingactivity.setStrtime(new Date());
        groupbuyingactivity.setEndtime(new Date(System.currentTimeMillis()+groupbuyingactivity.getStrgrouptime()*60*1000));
        groupbuyingactivity.setStatus(0);
        groupbuyingactivity.setNoclustering(0);

       groupbuyingactivityMapper.insert(groupbuyingactivity);
        Integer id = groupbuyingactivity.getId();
        Integer uid = groupbuyingactivity.getUid();
        UserGroup userGroup = new UserGroup();
        userGroup.setGid(id);
        userGroup.setUid(uid);
         userGroupMapper.insert(userGroup);
        rabbitTemplate.convertAndSend("day3-19","xty",userGroup.getUid()+"");

        return  R.OK("开团成功");
    }
}




